package com.github.edu.manager.fwdl.controller;

import com.github.admin.edu.assembly.common.entity.JsonArray;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.page.entity.Pager;
import com.github.edu.client.common.annotation.*;
import com.github.edu.client.common.controller.BaseController;
import com.github.edu.manager.fwdl.service.FwdlService;
import com.github.edu.manager.fwdl.service.FwflService;
import com.github.edu.manager.fwdl.service.GzmsService;
import com.github.edu.repair.api.entity.TBxFwdl;
import com.github.edu.repair.api.entity.TBxFwfl;
import com.github.edu.repair.api.entity.TBxGzms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-22
 */
@Controller
@RequestMapping("/uc/gzms")
public class GzmsController extends BaseController<TBxGzms,Integer> {

    @Autowired
    private GzmsService service;

    @Autowired
    private FwdlService fwdlService;

    @Autowired
    private FwflService fwflService;

    @ModelAttribute
    public void getAllFwdl(ModelMap map){
        List<TBxFwdl> list=fwdlService.getAllFwdl();
        map.put("fwdlList",list);
    }

    @QueryAll
    public String index(ModelMap modelMap, TBxGzms sysOauth, Pager pager) {
        if(null==pager){
            pager=new Pager();
        }
        pager.setDirection("ASC");
        String[] strs={"id"};
        pager.setOrder(strs);
        JsonArray<TBxGzms> jsonArray=service.getAllByPage(sysOauth,pager);
        if(null!=sysOauth&&null!=sysOauth.getFwdl()){
            List<TBxFwfl> lists=fwflService.getAll(sysOauth.getFwdl());
            modelMap.put("fwflList",lists);
        }
        return super.index(modelMap,sysOauth,jsonArray,"gzms","故障信息",
                "编码:id","故障名称:fwsm","所属区域:fwdlmc","当前状态:stateName");
    }

    @New
    public String add(ModelMap modelMap, TBxGzms sysOauth, HttpServletRequest request) {
        return super.newIndex(modelMap,sysOauth,"gzms","保存故障信息");
    }

    @Save
    public String saveOrUpdate(ModelMap modelMap, TBxGzms sysOauth, HttpServletRequest request) {
        sysOauth=service.saveOrUpdate(sysOauth);
        return super.saveObject(modelMap,sysOauth,"保存成功！","gzms","保存故障信息");
    }

    @Open
    public String openEntity(ModelMap modelMap,@PathVariable("id") Integer id) {
        TBxGzms application=service.getEntity(id);
        if(null!=application&&null!=application.getFwdl()){
            List<TBxFwfl> lists=fwflService.getAll(application.getFwdl());
            modelMap.put("fwflList",lists);
        }
        return super.openEntity(modelMap,application,"gzms","查看详情");
    }
    @Edit
    public String edit(ModelMap modelMap,@PathVariable("id") Integer s) {
        TBxGzms application=service.getEntity(s);
        if(null!=application&&null!=application.getFwdl()){
            List<TBxFwfl> lists=fwflService.getAll(application.getFwdl());
            modelMap.put("fwflList",lists);
        }
        return super.newIndex(modelMap,application,"gzms","修改故障信息");
    }

    @Delete
    public String delete( String ids) {
        return service.delete(ids);
    }

    @GetMapping("/ajax/fwfl")
    @ResponseBody
    public String getFwfl(@RequestParam("id") Integer id){
        List<TBxFwfl> list=fwflService.getAll(id);
        JsonArray<TBxFwfl> jsonArray=new JsonArray<>();
        jsonArray.setDataList(list);
        return JsonUtils.toJson(jsonArray);
    }
}
